package com.itheima.contoller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.City;
import com.itheima.service.Impl.JLServiceImpl;
import com.itheima.service.JLService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/cityServlet")
public class CityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端发送的id，根据id查询城市
        String id = req.getParameter("id");
        /*创建业务层对象*/
        JLService service = new JLServiceImpl();
        //调用业务层方法
        List<City> list=service.findCityById(Integer.parseInt(id));
        //将集合对象转成JSON格式的字符串写回前端
        ObjectMapper om=new ObjectMapper();
        String s = om.writeValueAsString(list);
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
